#include "ListObject.h"
#include "Global.h"
#include "Simon.h"
#include "Loadfile.h"
#include "BackGround.h"
#include "Fire.h"
#include "Brick.h"
#include "Candle.h"
#include "Door.h"
#include "Ground.h"
#include "Stair.h"
#include "trace.h"
ListObject::ListObject(void){}
ListObject::~ListObject(void){}
vector<string> temp;
void ListObject::load(int level)
{
	char path[40];
	
	sprintf_s(path, "map\\%d.txt", level);
	temp = readfiletxt(path);
	
}
// doc file lay ra tat ca cac doi tuong va map
void ListObject::Init(int level)
{
	char path[20];
	/*int uniquevalue[2000];*/
	index = 0;
	load(level);
	Global::rows = atoi(temp.at(0).c_str());
	Global::cols = atoi(temp.at(1).c_str());
	int left,top, uni = 0;
	Global::mapHeight = Global::rows * TILE_SIZE;
	Global::mapWidth = Global::cols * TILE_SIZE;
	
	Global::binaryTree = new BinaryTree(Global::mapWidth, Global::mapHeight);
	for (int i = 0; i < Global::rows; i++)
	{
		for (int j = 0; j < Global::cols; j++)
		{
			Global::mtbg[i][j] = atoi(temp.at(i*Global::cols+j+2).c_str());
			/*uniquevalue[uni] = atoi(temp.at(i*Global::cols+j+2).c_str());
			uni++;*/
		}
	}
	/*for (int i = 0; i < uni; i++)
	{
		for (int j = i+1; j < uni; j++)
		{
			if (uniquevalue[i]==uniquevalue[j])
			{
				uniquevalue[j] = uniquevalue[uni -1];
				uni--;
				j--;
			}
		}
	}*/

	//for (int i = 0; i < Global::rows; i++)
	//{
	//	for (int j = 0; j < Global::cols; j++)
	//	{
	//		left = j*TILE_SIZE;
	//		top = (Global::rows - i)*TILE_SIZE;// ve o tren truoc
	//		BackGround* bg = new BackGround(left,top,level, Global::mtbg[i][j]);
	//		Global::listBG.push_back(bg);
	//	}
	//}
	//for (int i = 0; i < uni; i++)
	//{
	//		BackGround* bg = new BackGround(32,32,level, uniquevalue[i]);
	//		
	//		Global::listBG.push_back(bg);

	//}
	for ( int a = Global::rows*Global::cols + 2; a < temp.size()-2 ; a += 4)
	{
		if (atoi(temp.at(a+1).c_str()) == _CANDLE)
		{
			Candle* candle = new Candle(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()));
			Global::listStaticObj.push_back(candle);
		}
		else if (atoi(temp.at(a+1).c_str()) == _FIRE)
		{
			Fire* fire = new Fire(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()));
			Global::listStaticObj.push_back(fire);
			
		}
		else if (atoi(temp.at(a+1).c_str()) == _BRICK)
		{
			Brick* brick = new Brick(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()));
			Global::listStaticObj.push_back(brick);
		}
		else if (atoi(temp.at(a+1).c_str()) == _GROUND)
		{
			Ground* ground = new Ground(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()));
			Global::listStaticObj.push_back(ground);
		}
		else if (atoi(temp.at(a+1).c_str()) == _DOOR)
		{
			Door* door = new Door(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()));
			Global::listStaticObj.push_back(door);
		}
		else if (atoi(temp.at(a+1).c_str()) == _STAIR_LEFT)
		{
			Stair* stair = new Stair(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()),atoi(temp.at(a+1).c_str()));
			Global::listStaticObj.push_back(stair);
		}
		else if (atoi(temp.at(a+1).c_str()) == _STAIR_RIGHT)
		{
			Stair* stair = new Stair(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()),atoi(temp.at(a).c_str()),atoi(temp.at(a+1).c_str()));
			Global::listStaticObj.push_back(stair);
		}
		/*else if (atoi(temp.at(a+1).c_str()) == _SIMON)
		{
			Global::SI->Init(atoi(temp.at(a+2).c_str()),atoi(temp.at(a+3).c_str()));
			Global::dynamicManager->add(Global::SI);
		}*/
		Global::roi->Init();
	}

}

void ListObject::RenderObj()
{
	list<Static*>::iterator it;
	for ( it = Global::listStaticObj.begin(); it!=Global::listStaticObj.end(); it++)
	{
		(*it)->Render();
	}

}